package com.mall.order.controller;


import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.mall.common.util.RespCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.mall.order.model.Order;
import com.mall.common.util.RespResult;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.mall.order.service.IOrderService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import java.util.Date;


/**
 * <p>
 * 订单表 控制器
 * </p>
 *
 * @author Darren
 * @since 2022-08-29
 */
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@Api(tags = "REST - 订单表")
@RestController
@RequestMapping("/order")
@CrossOrigin
public class OrderController {
    private final IOrderService service;

    @PostMapping
    @ApiOperation(value = "新增")
    public RespResult addOrder(@RequestBody Order order) {
        String userName = "gp";
        order.setUsername(userName);
        order.setCreateTime(new Date());
        order.setUpdateTime(order.getCreateTime());
        order.setId(IdWorker.getIdStr());
        order.setOrderStatus(0);
        order.setPayStatus(0);
        order.setIsDelete(0);

        boolean flag = service.addOrder(order);
        return flag ? RespResult.ok() : RespResult.error(RespCode.SYSTEM_ERROR);
    }

    @DeleteMapping("/delete/{id}")
    @ApiOperation(value = "根据id删除")
    public RespResult deleteById(@PathVariable("id") String id) {
        boolean flag = service.deleteById(id);
        return RespResult.ok(flag ? "删除成功" : "删除失败");
    }

    @PutMapping("/updateOrder")
    @ApiOperation(value = "更新")
    public RespResult updateOrder(@RequestBody Order order) {
        boolean flag = service.updateOrder(order);
        return RespResult.ok(flag ? "更新成功" : "更新失败");
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查询")
    public RespResult getById(@PathVariable("id") String id) {
        return RespResult.ok(service.getById(id));
    }

    @PostMapping("/selectList")
    @ApiOperation(value = "根据条件查询全部数据")
    public RespResult selectList(Order order) {
        return RespResult.ok(service.selectList(order));
    }

    @PostMapping("/selectPage")
    @ApiOperation(value = "分页查询")
    public RespResult selectPage(Page<Order> page, Order order) {
        return RespResult.ok(service.selectPage(page, order));
    }

}